﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using SimpleTaskManager.WebUI.Models;
using SimpleTaskManager.WebUI.Infrastructure.Abstract;

namespace SimpleTaskManager.WebUI.Controllers
{
    /// <summary>
    /// A controller to manage authentication and authorization
    /// </summary>
    public class AccountController : Controller
    {
        private IAuthProvider authProvider;

        /// <summary>
        /// Creates a new instance of the account controller
        /// </summary>
        /// <param name="authProvider">The auth provider to use</param>
        public AccountController(IAuthProvider authProvider)
        {
            this.authProvider = authProvider;
        }

        //
        // GET: /Account/LogOn
        /// <summary>
        /// Renders the log on page.
        /// </summary>
        /// <returns>The rendered view.</returns>
        public ActionResult LogOn()
        {
            return View();
        }

        //
        // POST: /Account/LogOn
        /// <summary>
        /// Handles a log on attempt.
        /// </summary>
        /// <param name="model">The data received from the logon form.</param>
        /// <param name="returnUrl">The Url which redirected the user to the login page.</param>
        /// <returns>On succesful login redirects to the original page else displays the logon form with the errors.</returns>
        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (authProvider.Authenticate(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    return Redirect(returnUrl ?? Url.Action("Index", "Account"));
                }
                else
                {
                    ModelState.AddModelError("", "Incorrect username or password");
                    return View();
                }
            }
            else
            {
                // If we got this far, something failed, redisplay form
                return View(model);
            }
        }

        //
        // GET: /Account/LogOff
        /// <summary>
        /// Signs out the current user, and redirects to the home page.
        /// </summary>
        /// <returns>Redirect to the home page.</returns>
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Home");
        }

        #region Status Codes
        private static string ErrorCodeToString(MembershipCreateStatus createStatus)
        {
            // See http://go.microsoft.com/fwlink/?LinkID=177550 for
            // a full list of status codes.
            switch (createStatus)
            {
                case MembershipCreateStatus.DuplicateUserName:
                    return "User name already exists. Please enter a different user name.";

                case MembershipCreateStatus.DuplicateEmail:
                    return "A user name for that e-mail address already exists. Please enter a different e-mail address.";

                case MembershipCreateStatus.InvalidPassword:
                    return "The password provided is invalid. Please enter a valid password value.";

                case MembershipCreateStatus.InvalidEmail:
                    return "The e-mail address provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidAnswer:
                    return "The password retrieval answer provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidQuestion:
                    return "The password retrieval question provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.InvalidUserName:
                    return "The user name provided is invalid. Please check the value and try again.";

                case MembershipCreateStatus.ProviderError:
                    return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                case MembershipCreateStatus.UserRejected:
                    return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

                default:
                    return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
            }
        }
        #endregion
    }
}
